import requests
from apiAutomation import app


class UserApi:
    def __init__(self, token):
        # 添加账户url
        self.add_user_url = app.BATH_URL + "/user/addUser"
        # 查询账户是否存在url
        self.query_user_exit_url = app.BATH_URL + "/user/checkUsername/{}"
        # 修改账户信息url
        self.update_user_url = app.BATH_URL + "/user/update"
        # 查询所有账户信息
        self.query_all_user_url = app.BATH_URL + "/user/queryAll"
        # 删除账户信息
        self.delete_user_url = app.BATH_URL + "/user/deleteUser"
        # 请求头
        self.headers = {
            "content-type": "applocation/json",
            "token": token
        }

    # 添加账户接口
    def add_user(self, username, password, role_id):
        data = {
            "username": username,
            "password": password,
            "role_id": role_id
        }
        return requests.post(url=self.add_user_url, headers=self.headers, json=data)

    # 查询账户是否存在接口
    def query_user_exit(self, username):
        new_url = self.query_user_exit_url.format(username)
        return requests.post(url=new_url, headers=self.headers)

    # 修改账户信息接口
    def update_user(self, id, uesrname, password):
        data = {
            "id": id
        }
        # 判断要不要传非必填参数username
        if uesrname is not None:
            data["username"] = uesrname
        # 判断要不要传非必填参数password
        if password is not None:
            data["password"] = password
        return requests.post(url=self.update_user_url, headers=self.headers, json=data)

    # 查询所有账户信息
    def query_all_user(self, pageNum, pageSize):
        data = {}
        # 判断要不要传非必填参数pageNum
        if pageNum is not None:
            data["pageNum"] = pageNum
        # 判断要不要传非必填参数pageSize
        if pageSize is not None:
            data["pageSize"] = pageSize
        return requests.post(url=self.query_all_user_url, headers=self.headers, json=data)

    # 删除账户
    def delete_user(self, id):
        data = {
            "id": id
        }
        return requests.post(url=self.delete_user_url, headers=self.headers, json=data)
